package pers.zhang.controller;

import org.dom4j.DocumentException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.zhang.dto.TextMessage;
import pers.zhang.util.CheckUtil;
import pers.zhang.util.MessageUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

/**
 * @Classname WeXinController
 * @Description TODO
 * @Date 2019/1/18 16:14
 * @Created by zhangkai
 */
@Controller
@RequestMapping("/wechat")
public class WeXinController {
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public void CheckToken(HttpServletRequest req, HttpServletResponse response) throws IOException {
        String signature=req.getParameter("signature");
        String timestamp=req.getParameter("timestamp");
        String nonce=req.getParameter("nonce");
        String echostr=req.getParameter("echostr");
        PrintWriter out = response.getWriter();
        if (CheckUtil.checkSignature(signature,timestamp,nonce))
        {
            out.print(echostr);
        }
    }
    @RequestMapping(method =RequestMethod.POST)
    @ResponseBody
    public  void GetMessage(HttpServletRequest req, HttpServletResponse response) throws IOException {
        req.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter printWriter=response.getWriter();
        try {
            Map<String,String> map=MessageUtil.xmlToMap(req);
            String ToUserName=map.get("ToUserName");
            String FromUserName=map.get("FromUserName");
//            String CreateTime=map.get("CreateTime");
            String MsgType=map.get("MsgType");
            String Content=map.get("Content");
//            String MsgId=map.get("MsgId");
            String message=null;
            if (MessageUtil.MESSAGE_TEXT.equals(MsgType))
            {//获取消息
//                 TextMessage textMessage=new TextMessage();
//                //接收方 测试
//                textMessage.setFromUserName(ToUserName);
//                textMessage.setToUserName(FromUserName);
//                textMessage.setMsgType("text");
//                textMessage.setCreateTime((int) new Date().getTime());
//                textMessage.setContent("您发送的消息是："+Content);
//                message=MessageUtil.textToXml(textMessage);
//                System.out.println(message);
//                printWriter.print(message);
                if ("1".equals(Content)){
                    message=MessageUtil.initText(ToUserName,FromUserName,MessageUtil.firstText());
               }else if("2".equals(Content))
                {
                    message=MessageUtil.initText(ToUserName,FromUserName,MessageUtil.firstText());
                }else if("3".equals(Content))
                {
                    message=MessageUtil.initText(ToUserName,FromUserName,Content);
                }
            }else if(MessageUtil.MESSAGE_EVENT.equals(MsgType))
            {//事件推送
                String eventType =map.get("Event");
                if (MessageUtil.MESSAGE_SUBSCRIBE.equals(eventType))
                {
                    message=MessageUtil.initText(ToUserName,FromUserName,MessageUtil.menuText());
               }
            }
            printWriter.print(message);
        } catch (DocumentException e) {
            e.printStackTrace();
        }finally {
            printWriter.close();
        }
    }
}
